#!/usr/bin/python
# -*- coding: utf-8 -*-  
import re  
import urllib  
import urllib2  
import cookielib  
from bs4 import BeautifulSoup

#创建一个cookie处理对象
cj = cookielib.CookieJar()  
#使用build_opener创建urlOpener，使用刚才创建的cookie处理对象作为cookies处理对象
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

#用户名和密码
form_data =  {'name': 'qing', 'pass': 'llqing2drupal', 'op':'登录'}  
#登录路径  
home_page = urllib2.urlopen('http://127.0.0.1:8080/drupal/user')
soup = BeautifulSoup(home_page)
form_data['form_build_id'] = soup.find('form',id='user-login').find('input',attrs={"name":"form_build_id"})['value']
print form_data
post_data = urllib.urlencode(form_data)
path = 'http://127.0.0.1:8080/drupal/user/login'  
opener.addheaders = [('User-agent', 'Opera/9.23')]  
urllib2.install_opener(opener)  
req = urllib2.Request(path, post_data)  
conn = urllib2.urlopen(req)
msg_soup = BeautifulSoup(coon)
print msg_soup.find('div',id='messages')

